package com.bit.demo.controller;

import com.bit.demo.model.UserInfo;
import com.bit.demo.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AliasFor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public boolean login(String userName, String password, HttpSession  session) {
        //是否为空
        if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return false;
        }

        UserInfo userInfo = userService.queryUserByName(userName);
        if (userInfo == null) {
            return false;
        }
        //校验密码
        if (userInfo != null && userInfo.getPassword().equals(password)) {
            session.setAttribute("session_user_key", userInfo);
            return true;
        }

        return false;
    }
}
